package stat;

public class SquareEquation {

	private double root[];

	private double a, b, c;

	public SquareEquation(double a, double b, double c) {
		this.a = a;
		this.b = b;
		this.c = c;
		root = new double[2];
	}

	public int numberOfRoots() {
		double delta = b * b - 4 * a * c;
		if (delta > 0) {
			return 2;
		} else if (delta == 0) {
			return 1;
		} else {
			return 0;
		}
	}

	public double getPositiveRoot() {
		if (numberOfRoots() < 1) {
			System.err.println("no root");
			return Double.MAX_VALUE;
		}

		double delta = Math.sqrt(b * b - 4 * a * c);
		root[0] = ((- b + delta) / 2 / a);
		root[1] = ((- b - delta) / 2 / 2);

//		System.out.println("square equation: " + a + "*x^2+" + b + "*x+" + c + "=0");
//		System.out.println("-- roots are: --");
//		System.out.println(" r1 = " + root[0]);
//		System.out.println(" r2 = " + root[1]);
		if (root[0] > 0)
			return root[0];
		else if (root[1] > 0)
			return root[1];
		else
			return Double.MAX_VALUE;
	}
}
